package com.isoft.student.service.impl;

import com.isoft.student.dao.AnswerMapper;
import com.isoft.student.dao.StudentMapper;
import com.isoft.student.eo.AnswerEOPage;
import com.isoft.student.model.Answer;
import com.isoft.student.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实现业务及流程接口当中的内容
 */
@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private StudentMapper studentMapper;
    /**
     * 答题表添加方式实现
     * @param answer 答题
     * @return 是否成功（-1 重复添加）
     */
    @Override
    public int AddAnswer(Answer answer) {
        int returnInt=0;
        //业务判断
        //（1）答题名称不能重复

        //(1)判断答题
         int TestpaperidCount=answerMapper.getTestpaperidCount(answer.getTestpaperid());
         if (TestpaperidCount<1){
             //2.答题信息数据添加
             int addAnswerInt=answerMapper.insertAnswer(answer);
             //3.返回值设置
             returnInt=addAnswerInt;
         }else {
             returnInt=-1;//有相同的信息名称
         }

        return returnInt;
    }

    /**
     * 答题更新
     * @param answer 答题
     * @return 更新是否成功（-1：更新执行SQL报错）
     */
    @Override
    public int UpdateAnswer(Answer answer) {
        int isUpdate=0;
        //1.没有相关业务流程的判断
        try {
            isUpdate=answerMapper.updateAnswer(answer);
        }catch (Exception e){
            isUpdate=-1;
            //打印更新报错的情况
            System.out.print("更新信息时执行SQL报错的原因为："+e.getMessage());
        }
        return isUpdate;
    }

    /**
     * 答题表删除
     * @param sid
     * @return
     */
    @Override
    public int DeleteAnswer(int sid) {
        int delAnswer=0;
        /*1.有关业务流程的判断
         * （1）删除答题不能与学生有关联
         * */
        try {
            //返回的结果总数量小于1，才可以删除
            if (studentMapper.getAnswerCount(sid)<1){
                delAnswer=answerMapper.delAnswer(sid);
            }
        }catch (Exception e){
            delAnswer=-1;
            System.out.print("删除角色信息室执行SQL时报错，信息为："+e.getMessage());
        }
        return delAnswer;
    }

    @Override
    public List<AnswerEOPage> getAnswerWhereAll(AnswerEOPage answerEOPage) {
        List<AnswerEOPage> listAnswerEo=answerMapper.getAnswerWhereAll(answerEOPage);
        return listAnswerEo;
    }
}
